package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.model.JdActivityPhones;
import com.company.project.model.JdRegions;
import com.company.project.service.JdActivityPhonesService;
import com.company.project.service.JdRegionsService;
import com.company.project.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by CodeGenerator on 2019/07/25.
 */
@RestController
@RequestMapping("/jd/activity/phones")
public class JdActivityPhonesController {

	@Resource
	private JdActivityPhonesService jdActivityPhonesService;

	@Resource
	private JdRegionsService jdRegionsService;


	@GetMapping("/list")
	public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size)
			throws IOException {

		InputStream ips=new FileInputStream("/Users/72cy-0101-01-0009/Downloads/pa.xls");
		HSSFWorkbook wb=new HSSFWorkbook(ips);
		ExcelExtractor ex=new ExcelExtractor(wb);
		ex.setFormulasNotResults(true);
		ex.setIncludeSheetNames(false);
		String text=ex.getText();
		// System.out.println(text);
		int i = 0;
		int j = 0;
		String[] split = text.split("\n");

		List<JdActivityPhones> list = new ArrayList<>();

		for (String s : split) {
			i ++;
			j ++;
			String[] _split = s.split("\t");

			// 判断存在 字母
			String phoneOld = _split[2];

			String phone = phoneOld.replaceAll(" ", "");

			boolean numeric = StringUtils.isNumeric(phone);
			if (!numeric) {
				continue;
			}

			String prePhone = phone.substring(0, 7);
			String province = "未知";
			String city = "未知";

			// JdRegions jdRegions = jdRegionsService.findRegionsByPrePhone(prePhone);
//			if (jdRegions != null) {
//				province = jdRegions.getProvince();
//				city = jdRegions.getCity();
//			}

			JdActivityPhones jdActivityPhones = new JdActivityPhones();
			jdActivityPhones.setId(StringUtil.getUUID());
			jdActivityPhones.setPhone(phone);
			jdActivityPhones.setProvince("");
			jdActivityPhones.setCity("");
			jdActivityPhones.setNumber(Integer.valueOf(prePhone));
			list.add(jdActivityPhones);

			if (j == 2000) {
				jdActivityPhonesService.insertForeach(list);
				j = 0;
				list = new ArrayList<>();
			}

			if (phone.equals("18971490807")) {
				jdActivityPhonesService.insertForeach(list);
			}

		}

		System.out.println("------- " + i);

		return null;
	}


	//通过对单元格遍历的形式来获取信息 ，这里要判断单元格的类型才可以取出值
	public static void readTable() throws Exception{
		InputStream ips=new FileInputStream("d://test2-1.xls");
		HSSFWorkbook wb=new HSSFWorkbook(ips);
		HSSFSheet sheet=wb.getSheetAt(0);
		for(Iterator ite = sheet.rowIterator(); ite.hasNext();){
			HSSFRow row=(HSSFRow)ite.next();
			System.out.println();
			for(Iterator itet=row.cellIterator();itet.hasNext();){
				HSSFCell cell=(HSSFCell)itet.next();
				switch(cell.getCellType()){
					case HSSFCell.CELL_TYPE_BOOLEAN:
						//得到Boolean对象的方法
						System.out.print(cell.getBooleanCellValue()+" ");
						break;
					case HSSFCell.CELL_TYPE_NUMERIC:
						//先看是否是日期格式
						if(HSSFDateUtil.isCellDateFormatted(cell)){
							//读取日期格式
							System.out.print(cell.getDateCellValue()+" ");
						}else{
							//读取数字
							System.out.print(cell.getNumericCellValue()+" ");
						}
						break;
					case HSSFCell.CELL_TYPE_FORMULA:
						//读取公式
						System.out.print(cell.getCellFormula()+" ");
						break;
					case HSSFCell.CELL_TYPE_STRING:
						//读取String
						System.out.print(cell.getRichStringCellValue().toString()+" ");
						break;
				}
			}
		}

	}

}
